<?php
namespace app\wechat\controller;
use think\View;

class Wallet extends Common
{
    // 用户钱包首页
    public function index()
    {
        $id = session('member_id');
        // 用户余额
        $money = db('member')->where('id', $id)->value('money');

        // 获取用户银行卡列表
        $bank_num = db('member_fund_account')->where('member_id', $id)->where('is_band', 1)->count();

        $this->assign('data',[
            'money' => $money,
            'bank_num' => $bank_num
        ]);
        return view();
    }

    // 交易明细页面
    public function detail() {
        if (request()->isAjax()) {
            $param['member_id'] = session('member_id');
            $param['type'] = input('param.type');
            $data = call_interface('member_money_bill', $param);
            $view = new View();
            if ($data['code'] == 200) {
                $view->assign('data', $data['data']['data']);
            } else {
                $view->assign('data', []);
            }
            $tpl = $view->fetch('wallet/detail_tpl');
            $data['tpl'] = $tpl;
            return json($data);
        }
        return view();
    }

    // 待结算金额明细页面
    public function detail_f() {
        if (request()->isAjax()) {
            $param['member_id'] = session('member_id');
            $param['type'] = input('param.type');
            $data = call_interface('member_money_bill_f', $param);
            $view = new View();
            if ($data['code'] == 200) {
                $view->assign('data', $data['data']['data']);
            } else {
                $view->assign('data', []);
            }
            $tpl = $view->fetch('wallet/detail_tpl_f');
            $data['tpl'] = $tpl;
            return json($data);
        }
        return view();
    }

    // 账单明细详情
    public function detail_info() {
        $param = [
            'detail_id' => input('param.id'),
            'member_id' => session('member_id')
        ];
        $data = call_interface('get_detail_info', $param);
        if ($data['code'] == 200) {
            $this->assign('data', $data['data']);
            return view();
        } else {
            return view('public/error');
        }
    }

    // 银行卡列表页面
    public function banks() {
        $param['member_id'] = session('member_id');
        $type = input('param.type');
        $banks = call_interface('get_member_fund_account', $param);
        $this->assign('banks', $banks['data']);
        $this->assign('type', $type);
        return view();
    }

    // 添加银行卡列表页面
    public function save_bank() {
        $fund_account_id = input('param.fund_account_id', 0);
        if (request()->isPost()) {
            $type = input('param.account_type'); // 银行卡类型
            switch ($type) {
                case 1: //  添加支付宝
                    break;
                case 2: // 添加微信
                    break;
                case 3:
                    $param = [
                        'member_id' => session('member_id'),
                        'type' => 3,
                        'account' => input('param.account'),
                        'phone' => input('param.phone'),
                        'householder' => input('param.householder'),
                        'id_card' => input('param.id_card'),
                        'fund_account_id' => input('param.fund_account_id'),
                        'source' => 0
                    ];
                    $data = call_interface('save_member_fund_account', $param);
                    break;
                default:
                    break;
            }
            return json($data);
        } else {
            $member_fund_account = [];
            if ($fund_account_id > 0) {
                $member_fund_account = db('member_fund_account')->where('id', $fund_account_id)->find();
            }
            $this->assign('member_fund_account', $member_fund_account);

            $url = input('param.url', 2);
            if ($url == 1) {
                $url = url('member_role_info/certification');
            } else if ($url == 3){
                $url = url('wallet/withdraw');
            } else {
                $url = url('wallet/banks');
            }

            // 检查是否实名认证
            $param = [
                'member_id' => session('member_id'),
            ];
            $certification = call_interface('certification_info', $param);
            $this->assign('certification', $certification['data']);
            $this->assign('url', $url);
            return view();
        }
    }

    // 银行卡解除绑定
    public function relieve() {
        $param = [
            'member_id' => session('member_id'),
            'fund_account_id' => input('param.bank_id')
        ];
        $data = call_interface('relieve_bank_band', $param);
        return json($data);
    }

    // 充值
    public function recharge() {
        $type = input('param.type', 0);
        $this->assign('type', $type);
        return view();
    }

    // 提现
    public function withdraw() {
        $type = input('param.type');
        if( request()->isAjax()) {
            $param = [
                'member_id' => session('member_id'),
                'type' => $type,
                'fund_account' => input('param.account'),
                'fund_account_id' => input('param.fund_account_id'),
                'fund_account_type' => input('param.fund_account_type'),
                'code' => input('param.code'),
                'money' => input('param.money'),
                'withdraw_poundage' => input('param.withdraw_poundage')
            ];
            $data = call_interface('member_withdraw', $param);
            return json($data);
        } else {
            $param = [
                'member_id' => session('member_id'),
                'type' => 0
            ];
            $data = call_interface('withdraw_index', $param);
            //dump($data);
            if ($data['code'] != 200) {
                return view('public/error');
            }
            $this->assign('data', $data['data']);
            
            // 检查是否实名认证
            $param = [
                'member_id' => session('member_id'),
            ];
            $certification = call_interface('certification_info', $param);
            $this->assign('certification', $certification['data']);

            $this->assign('type', $type);
            return view();
        }
        
    }
}
